package cn.itsuorce.controller;

import cn.itsuorce.domain.Department;
import cn.itsuorce.query.DepartmentQuery;
import cn.itsuorce.service.IDepartmentService;
import cn.itsuorce.utils.AjaxResult;
import cn.itsuorce.utils.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @PutMapping("/department")
    @ApiOperation(value = "新增或修改部门信息", tags = "新增或修改部门信息")
    public AjaxResult save(@RequestBody Department department) {
        try {
            System.out.println(department);
            departmentService.save(department);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！" + e.getMessage());
        }
    }

    /**
     * 查询部门唯一
     * @param sn
     * @param id
     * @return
     */
    @GetMapping("/department/findBySn")
    public AjaxResult findBySn(String sn,String id) {
        Department department = departmentService.findBySn(sn,id);
        System.out.println(department);
            if (department!=null){
                return AjaxResult.me().setSuccess(false).setMsg("该部门已存在");
            }
            return AjaxResult.me();
    }
    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/department/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            departmentService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！" + e.getMessage());
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/department/batch")
    public AjaxResult delete(@RequestBody List<Long> ids) {
        try {
            departmentService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！" + e.getMessage());
        }
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/department/{id}")
    public Department findOne(@PathVariable("id") Long id) {
        return departmentService.findOne(id);
    }

    /**
     * 查询所有，支持高级查询的条件
     *
     * @param departmentQuery
     * @return
     */
    @PostMapping("/department")
    public List<Department> findAll(@RequestBody DepartmentQuery departmentQuery) {
        return departmentService.findAll(departmentQuery);
    }

    /**
     * 分页查询
     *
     * @param departmentQuery
     * @return
     * @RequestBody 表示从请求消息体中接收前端发送过来的json字符串，并且使用jackson工具包自动转化为所需要的实体类类型的对象
     */
    @PatchMapping("/department")
    public AjaxResult page(@RequestBody DepartmentQuery departmentQuery) {
        try {
            System.out.println(departmentQuery);
            Page<Department> page = departmentService.findPage(departmentQuery);
            return AjaxResult.me().setData(page);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败！" + e.getMessage());
        }
    }
}